package org.mall.modules.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mall.common.api.vo.Result;
import org.mall.common.aspect.annotation.AutoLog;
import org.mall.common.util.json.JacksonUtils;
import org.mall.modules.shop.entity.Shop;
import org.mall.modules.shop.entity.ShopClerkRelationShip;
import org.mall.modules.shop.entity.ShopClerkRelationShipDTO;
import org.mall.modules.shop.service.IShopClerkRelationShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * @Auther: LL
 * @Date: 2019/10/30 15:46
 */
@Slf4j
@Api(tags = "商铺")
@RestController
@RequestMapping("/shopclerk")
public class ShopClerkController {

    @Autowired
    private IShopClerkRelationShipService shopClerkRelationShipService;
    /**
     * 通过id删除
     * @param shopClerkRelationShip
     * @return
     */
    @AutoLog(value = "商铺-通过店员id删除店员")
    @ApiOperation(value = "商铺-通过店员id删除", notes = "商铺-通过店员id删除")
    @PostMapping(value = "/merchant/delete")
    public Result<?> delete (@Valid @RequestBody ShopClerkRelationShip shopClerkRelationShip, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.error(JacksonUtils.toJSONString(bindingResult.getAllErrors()));
        }
            try {
                shopClerkRelationShipService.deleteOneShopClerk(shopClerkRelationShip.getShopId(),shopClerkRelationShip.getClerkId());
            } catch (Exception e) {
                log.error("删除失败"+e.getMessage());
                return Result.error("删除失败!");
            }
        return Result.ok("删除成功!");
    }
    /**
     * 批量删除
     *
     * @param shopClerkRelationShipDTO
     * @return
     */
    @AutoLog(value = "商铺-批量删除")
    @ApiOperation(value = "商铺-批量删除", notes = "商铺-批量删除")
    @PostMapping(value = "/merchant/deleteBatch")
    public Result<Shop> deleteBatch(@Valid @RequestBody ShopClerkRelationShipDTO shopClerkRelationShipDTO, BindingResult bindingResult) {
        Result<Shop> result = new Result<Shop>();
        if(bindingResult.hasErrors()){
            return result.error500(JacksonUtils.toJSONString(bindingResult.getAllErrors()));
        }
        if ((shopClerkRelationShipDTO.getShopId() == null || "".equals(shopClerkRelationShipDTO.getShopId().trim()))&&(shopClerkRelationShipDTO.getClerkId() == null || "".equals(shopClerkRelationShipDTO.getClerkId().trim()))) {
            result.error500("参数不识别！");
        } else {
            this.shopClerkRelationShipService.deleteListShopClerk(shopClerkRelationShipDTO.getShopId(),Arrays.asList(shopClerkRelationShipDTO.getClerkId().split(",")));
            result.success("删除成功!");
        }
        return result;
    }
}
